package com.api.controller;

import com.api.core.model.FormAjaxReturn;
import com.api.core.util.RspUtil;
import com.api.core.util.WebUtil;
import com.api.entity.SystemUser;
import com.api.service.ISystemMenuService;
import com.api.service.ISystemUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 不过滤 返回页面
 */
@Slf4j
@Controller
public class IndexController {

    @Resource
    private ISystemMenuService sysMenuService;

    @Resource
    ISystemUserService systemUserService;

    /**
     * 测试用
     * @param request
     * @return
     */
//    @RequestMapping("/index")
//    public String index(HttpServletRequest request){
//        return "index";
//    }

    @RequestMapping("/")
    public String login(HttpServletRequest request){
        SystemUser userInfo = (SystemUser)request.getSession().getAttribute("userInfo");
        if (null!=userInfo){
            return "index";
        }
        return "login";
    }

    @RequestMapping(value="/not_found")
    public String not_found(){
        return "404";
    }

    @GetMapping("/menu")
    public void menu(HttpServletResponse response) {
        RspUtil.returnsMsg(response,sysMenuService.menu());
    }

    @RequestMapping("/login")
    public void menu(HttpServletResponse response, HttpServletRequest request, SystemUser user) {
        FormAjaxReturn arf = new FormAjaxReturn();
        SystemUser systemUser = systemUserService.userLogin(user);
        if (null!=systemUser){
            HttpSession session = request.getSession();
            session.setMaxInactiveInterval(30*60);//以秒为单位，即在没有活动30分钟后，session将失效
            session.setAttribute("userInfo",systemUser);
            arf.setMessage("登录成功!");
            WebUtil.returnMsg(response, arf);
            return;
        }
        arf.setMessage("用户名或密码错误!");
        arf.setStatusCode(400);
        WebUtil.returnMsg(response, arf);
    }

    @RequestMapping("/outLogin")
    public void outLogin(HttpServletResponse response, HttpServletRequest request) {
        FormAjaxReturn arf = new FormAjaxReturn();
        request.getSession().invalidate();
        arf.setMessage("退出成功!");
        WebUtil.returnMsg(response, arf);
    }

}
